package com.jade.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.jade.common.config.Global;
import com.jade.common.core.controller.BaseController;
import com.jade.common.utils.StringUtils;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkNotice;
import com.jade.linkong.domain.LkProcessTaskFill;
import com.jade.linkong.domain.LkProject;
import com.jade.linkong.service.ILkNoticeService;
import com.jade.linkong.service.ILkProcessTaskFillService;
import com.jade.linkong.service.ILkProjectService;
import com.jade.system.domain.SysMenu;
import com.jade.system.domain.SysUser;
import com.jade.system.service.ISysMenuService;
import com.jade.system.service.ISysNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * 首页 业务处理
 * 
 * @author jade
 */

@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ILkProjectService projectService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private ILkNoticeService noticeService;
    @Autowired
    private ILkProcessTaskFillService lkProcessTaskFillService;

    // 系统首页
    @GetMapping("/in")
    @ResponseBody
    public String in()
    {
    	String ctx = getRequest().getContextPath();
	    if(ctx == "/") ctx = "";
	    String indexUrl = Global.getConfig("shiro.user.indexUrl");
	    String in = "<script> location.href = '"+ctx+indexUrl+"'; </script>";
	    String agent=this.getRequest().getHeader("User-Agent").toLowerCase();
	    if(agent!=null){
	    	  if( agent.indexOf("android")>0 || agent.indexOf("iphone")>0){
	    		  in = "<script> location.href = '"+ctx+"/index_m'; </script>";
	    	  }
	       }
        return in;
        
    }

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("demoEnabled", Global.isDemoEnabled());
        String toPage = "index";
        LkProcessTaskFill lkProcessTaskFill = new LkProcessTaskFill();
        lkProcessTaskFill.setFillStatus("1");
        lkProcessTaskFill.setProjectLeaderPhone(ShiroUtils.getLoginName());
        List<LkProcessTaskFill> list = lkProcessTaskFillService.selectLkProcessTaskFillList(lkProcessTaskFill);
        mmap.put("taskFillCount",list.size());
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        if ( StringUtils.isNotNull(roleKey) && (roleKey.equals("company") || roleKey.equals("dept"))){
            mmap.put("in","index_u");
        }else{
            mmap.put("in","index_d");
        }
	    String agent=this.getRequest().getHeader("User-Agent").toLowerCase();
	    if(agent!=null){
    	  if( agent.indexOf("android")>0 || agent.indexOf("iphone")>0){
    		  toPage = "index_m";
    	  }
	    }
        return toPage;
    }
   
    // 系统首页
    @GetMapping("/index_d")
    public String index2(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        PageHelper.startPage(1, 5, "ln.create_time desc");
        List<LkNotice> lkNotices = noticeService.selectLkNoticeList(new LkNotice());
        mmap.put("menus", menus);
        mmap.put("user", user);
        Map<String,Object> map = projectService.getCount();
        mmap.put("p_count", map.get("p_count"));
        mmap.put("total_count", map.get("total_count"));
        mmap.put("red_count", map.get("red_count"));
        mmap.put("yel_count", map.get("yel_count"));
        mmap.put("sys_notices", lkNotices);
        String toPage = "index_d";
        return toPage;
    }

    @GetMapping("/index_u")
    public String indexu(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        PageHelper.startPage(1, 5, "ln.create_time desc");
        List<LkNotice> lkNotices = noticeService.selectLkNoticeList(new LkNotice());
        mmap.put("menus", menus);
        mmap.put("user", user);
        Map<String,Object> map = projectService.getCount();
        mmap.put("p_count", map.get("p_count"));
        mmap.put("total_count", map.get("total_count"));
        mmap.put("red_count", map.get("red_count"));
        mmap.put("yel_count", map.get("yel_count"));
        mmap.put("sys_notices", lkNotices);

        String toPage = "index_u";
        return toPage;
    }

    @GetMapping("/index_p")
    public String indexp(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        PageHelper.startPage(1, 5, "ln.create_time desc");
        List<LkNotice> lkNotices = noticeService.selectLkNoticeList(new LkNotice());
        mmap.put("menus", menus);
        mmap.put("user", user);
        Map<String,Object> map = projectService.getCount();
        mmap.put("p_count", map.get("p_count"));
        mmap.put("total_count", map.get("total_count"));
        mmap.put("red_count", map.get("red_count"));
        mmap.put("yel_count", map.get("yel_count"));
        mmap.put("sys_notices", lkNotices);
        String toPage = "index_p";
        return toPage;
    }

    @GetMapping("/toPrejectDetail")
    public String toPrejectDetail( String proId, ModelMap mmap){

        LkProject lkProject = projectService.selectLkProjectById(proId);
        mmap.put("companyName", lkProject.getCompanyName());
        mmap.put("proName", lkProject.getProName());
        mmap.put("remark", lkProject.getRemark());
        mmap.put("proId", proId);
        return "project-progress";
    }

    //jade_UI首页
    @GetMapping("/jade")
    public String jade(ModelMap mmap)
	{   
    	SysUser user = ShiroUtils.getSysUser();
    	mmap.put("user", user);
    	String ctx = getRequest().getContextPath();
	    if(ctx == "/") ctx = "";
	    mmap.put("ctx", ctx );
	    String toPage = "ui/index";
	    String agent=this.getRequest().getHeader("User-Agent").toLowerCase();
	    if(agent!=null){
    	  if( agent.indexOf("android")>0 || agent.indexOf("iphone")>0){
    		  toPage = "index_m";
    	  }
	    }
        return toPage;
    }
    
    //移动首页
    @RequiresPermissions("system:com:view")
    @GetMapping("/index_m")
    public String index_m(ModelMap mmap)
	{   
    	SysUser user = ShiroUtils.getSysUser();
    	mmap.put("user", user);
    	String ctx = getRequest().getContextPath();
	    if(ctx == "/") ctx = "";
	    mmap.put("ctx", ctx );
        return "index_m";
    } 
    //大屏首页
    @GetMapping("/index_big")
    public String index_big(ModelMap mmap)
	{   
    	SysUser user = ShiroUtils.getSysUser();
    	mmap.put("user", user);
    	String ctx = getRequest().getContextPath();
	    if(ctx == "/") ctx = "";
	    mmap.put("ctx", ctx );
	    String toPage = "index_big";
	    String agent=this.getRequest().getHeader("User-Agent").toLowerCase();
	    if(agent!=null){
    	  if( agent.indexOf("android")>0 || agent.indexOf("iphone")>0){
    		  toPage = "index_m";
    	  }
	    }
        return toPage;
    }
    //jade_UI 获取json
    @PostMapping("/getJson")
    @ResponseBody
    public String getJson(ModelMap mmap)
    {
        String json =  getRequest().getParameter("json");
        if(json == null) 
        	json = "";
        else{
        	String profile = Global.getConfig("jade.profile");
        	json = Read(profile+File.separatorChar + "json" + File.separatorChar +json+".json");
        }
        return json;
    }
    
    // jade_UI 获取模板
    @GetMapping("/toPage")
    public String toPage(ModelMap mmap)
    {
    	SysUser user = ShiroUtils.getSysUser();
    	mmap.put("user", user);
    	String ctx = getRequest().getContextPath();
        if(ctx == "/") ctx = "";
        mmap.put("ctx", ctx );
        String page =  getRequest().getParameter("page");
        return page;
    }
    
    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        mmap.put("version", Global.getVersion());
        return "main";
    }
    public String Read(String Path){
      System.out.println("Read:"+Path);
  	  StringBuffer sb = new StringBuffer();
  	  BufferedReader Br = null;
  	  String   line   =   null;
  	  try {
  		  File file = new File(Path);
  		  if(file.exists()){
  		     Br = new BufferedReader( new InputStreamReader(new FileInputStream(Path),"UTF-8"));   
             while((line = Br.readLine())!=null)   
             {   
        	     sb.append("\n").append(line);
             }
  		  }
  	  }catch (Exception e) {
  			System.out.print("WriteText"+e.toString());
  	  }finally{
  			if(Br!=null)
  				try {
  					Br.close();
  				} catch (IOException e) {
  					// TODO Auto-generated catch block
  					e.printStackTrace();
  				}
  		}
  	  line = sb.toString();
  	  if(line.length()>1){
  	     return line.substring(1);
  	  }else
  	     return "";
  }
}
